package com.example.minefieldkeepercloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.bean.MineFieldBean;
import com.example.minefieldkeepercloud.bean.StaffBean;
import com.example.minefieldkeepercloud.util.TimeUtil;
import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 员工列表
 * @Author:Created by  ygy
 * @time:2019/10/8
 */
public class StaffAdapter extends ViewHolderRecyclerAdapter<StaffBean.DataBean> {




    private boolean isCheckBox=false;
    private  List<StaffBean.DataBean> listData;
    // 存储勾选框状态的map集合

    private Map<Integer,Boolean> isCheck = new HashMap();


    public boolean isCheckBox() {
        return isCheckBox;
    }

    public void setCheckBox(boolean checkBox) {
        isCheckBox = checkBox;
    }

    private OnCheckBoxListener onCheckBoxListener;

    public Map<Integer, Boolean> getIsCheck() {
        return isCheck;
    }

    public void setIsCheck(Map<Integer, Boolean> isCheck) {
        this.isCheck = isCheck;
    }

    public StaffAdapter(Context context, List<StaffBean.DataBean> listData, OnCheckBoxListener onCheckBoxListener) {
        super(context, listData);
        this.listData=listData;
        this.onCheckBoxListener=onCheckBoxListener;
       initCheck(false);
    }

    /**
     * 初始化map集合
     * @param checked
     */
    public void initCheck(boolean checked) {
        for (int i = 0; i < listData.size(); i++) {
            isCheck.put(i,checked);
        }
    }


    @Override
    public View buildConvertView(LayoutInflater layoutInflater, int viewType) {
        return layoutInflater.inflate(R.layout.item_staff,null);
    }



    @Override
    public void bindViewDatas(final ViewHolder holder, final StaffBean.DataBean dataBean, final int position) {




        holder.setText(R.id.tv_FactoryName,dataBean.getFactoryName());
        holder.setText(R.id.tv_Name,dataBean.getName());
        holder.setText(R.id.tv_IPSegment,dataBean.getIPSegment());
        try {
            holder.setText(R.id.tv_Startworkinghours, TimeUtil.dealDateFormatNomm(dataBean.getStartworkinghours()));
            holder.setText(R.id.tv_Endworkinghours, TimeUtil.dealDateFormatNomm(dataBean.getEndworkinghours()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        holder.setText(R.id.tv_FullName,dataBean.getFullName()+"("+dataBean.getRoleName()+")");


        //列表单条点击
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });

        if (!isCheckBox())
        {
          //不显示复选框
            holder.getView(R.id.checkbox).setVisibility(View.GONE);
        }else {
            holder.getView(R.id.checkbox).setVisibility(View.VISIBLE);
        }

        ((CheckBox)holder.getView(R.id.checkbox)).setOnCheckedChangeListener(null);
        if (isCheck.get(position) != null)
        {
            ((CheckBox)holder.getView(R.id.checkbox)).setChecked(isCheck.get(position));
        }

        ((CheckBox)holder.getView(R.id.checkbox)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                isCheck.put(position,b);


                    onCheckBoxListener.clickCheck();

            }
        });
    }

    /**
     * 加载更多更新数据
     */
    public  void moreMinerNotifyDataSetChanged()
    {

        for (int i = 0; i <listData.size(); i++) {
            if (isCheck.get(i)==null)
            {
                isCheck.put(i,false);
            }
        }
        notifyDataSetChanged();
    }
    /**
     * 刷新更新数据
     */
    public  void refrashMinerNotifyDataSetChanged()
    {
        isCheck.clear();
        for (int i = 0; i <listData.size(); i++) {

                isCheck.put(i,false);

        }
        notifyDataSetChanged();
    }

    /**
     * 点击回调
     */
    public interface OnCheckBoxListener {
        public void clickCheck();

    }
}
